package com.hsxc.cdz.monitor.bean.second;

import com.hsxc.cdz.base.util.BytesUtil;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.FSM.CachePile;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;

public class HeartSerMsg extends SecMessage {

	private byte state;//1:空闲中，2：充电中，3：故障中，4：维护中，5：满电占用中
                       //6：未满电占用中    未满电占用中：在未充满电的情况下停止充电，但充电枪尚未拔出
	public static final int length = 15;
	public HeartSerMsg(){
		setVersion(final_version);
		setFrame(frame_heart_ser);
	} 
	
	public HeartSerMsg(String pileNO){
		this();
		setPileNO(pileNO);
	}
	/***************************************************************/
	@Override
	public byte[] getMessage() {
		ByteBuf bytes = Unpooled.buffer(length);
		byte[] result;
		bytes.writeBytes(BytesUtil.strToBinBytes(isStart, 1));
		bytes.writeBytes(BytesUtil.intToBytesDesc(length, length_length));
		bytes.writeBytes(BytesUtil.intToBytesSeq(getVersion(),length_version));
		bytes.writeBytes(BytesUtil.intToBytesDesc(getSerIndex(), length_seIndex));
		bytes.writeBytes(BytesUtil.intToBytesDesc(getPileIndex(), length_pileIndex));
		bytes.writeBytes(BytesUtil.strToBcdBytes(getPileNO(), length_pileNO));
	    bytes.writeBytes(BytesUtil.intToBytesSeq(getFrame(), length_frame));
	    bytes.writeBytes(new byte[]{state});
	    result = new byte[bytes.readableBytes()];
		bytes.readBytes(result);
		ReferenceCountUtil.release(bytes);
		return result;
	}

	@Override
	public boolean execute() {
		CachePile pile = GlobalManager.pileManager.getPile(getPileNO());
	//	setPileIndex(pile.getPileIndex());
	//	int serIndex = pile.getSerIndex()+1;
	//	setSerIndex(serIndex);
	//	pile.setSerIndex(serIndex);
	//	state = pile.getState();
		pile.getChannel().writeAndFlush(this);
		return true;
	}

	/*****************************************************************/
	//get() and set()
	public byte getState() {
		return state;
	}

	public void setState(byte state) {
		this.state = state;
	}
}
